local audio = require("framework.audio")
local audioPath = require("app.res.audio.config")
local baseMatrix = require("app.views.matrix")
local M = class("MatrixRight", baseMatrix)

function M:ctor(obj,data)
    self._touchAble = true
    self.m_success = data.success
    self.m_lose = data.lose
    self.m_addScore = data.addScore
    self.super.ctor(self)
    self.m_isOpened = {false,false,false,false, 
                        false,false,false,false, 
                        false,false,false,false, 
                        false,false,false,false}

    self:setTouchEnabled(true)
    self:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
            if event.name == "began" then
                local pos = self:convertToNodeSpace(cc.p(event.x,event.y))
                local coord = {x=math.ceil(pos.x/(self.size.width+self.padding_x)),y=math.ceil(pos.y/(self.size.height+self.padding_y))}
                
                local x,y = (coord.x-1)*self.size.width+self.size.width*0.5+(coord.x-1)*self.padding_x,(coord.y-1)*self.size.height+self.size.height*0.5+(coord.y-1)*self.padding_y
                
                local dis = math.sqrt((pos.x-x)*(pos.x-x)+(pos.y-y)*(pos.y-y))
                if dis<self.size.width*0.5 then
                    self:onTouch(coord)
                else
                    -- print("外边")
                end
            end
        end)
end

function M:onTouch(coord)
    local index = self:coord2index(coord)
    -- print(index,"coord.x= "..coord.x,"coord.y= "..coord.y,tostring(self._touchAble))
    if not self._touchAble or self._isTouching then
        return
    end
    local gird = self.m_girds[index]
    if self.m_isOpened[index]then
        return
    end
    self.m_isOpened[index] = true
    self._isTouching = true  --这次点击完成了，才能进行下一次点击
    if self.m_data[index]==0 then
        self:touch_X(gird)
    else
        self:touch_O(gird)
    end
end

function M:touch_O(gird)
    audio.playSound(audioPath.clickRight)
    if self.m_addScore and type(self.m_addScore)=="function" then
        local acc = 1
        for i=1,#self.m_data do
            if self.m_data[i]==1 and not self.m_isOpened[i] then
                acc = acc + 1
            end
        end
        self.m_addScore(acc)
    end
    gird:hide()
    self:setTex_O(gird)
    local duration = 0.2
    local seq = transition.sequence({     
                                          cc.ScaleTo:create(0,0.1,0.1),
                                          cc.Show:create(),
                                          cc.ScaleTo:create(duration,1.2,1.2),
                                          cc.ScaleTo:create(duration*0.4,1,1),
                                          cc.CallFunc:create(function ( ... )
                                              self:checkTouch()
                                          end)
                                      })
    gird:runAction(seq)
end

function M:touch_X(gird)
    audio.playSound(audioPath.clickWrong)
    gird:hide()
    self:setTex_X(gird)
    local duration = 0.2
    local seq = transition.sequence({     
                                          cc.ScaleTo:create(0,0.1,0.1),
                                          cc.Show:create(),
                                          cc.ScaleTo:create(duration,1.2,1.2),
                                          cc.ScaleTo:create(duration*0.4,1,1),
                                          cc.CallFunc:create(function ( ... )
                                              self:checkTouch()
                                          end)
                                      })
    gird:runAction(seq)
end

function M:updateMatrix()
    
end

function M:showGirds()
    self._touchAble = true
    self._isTouching = true
    self:updateMatrix()
    local duration = 0.2
    for i=1,#self.m_girds do
        local ani = transition.sequence{cc.FadeOut:create(0),cc.Show:create(),cc.FadeIn:create(duration)}
        self.m_girds[i]:runAction(ani)
    end
    self:performWithDelay(function ( ... )
        self._isTouching = false
    end, duration)
    return self
end

function M:checkTouch()
    self._isTouching = false
    local isSuccess = true
    for i=1,#self.m_data do
        if self.m_data[i]==0 and self.m_isOpened[i] then --存在是X却被点击了
            -- print("玩儿完了")
            if self.m_lose and type(self.m_lose)=="function" then
                self._touchAble = false
                self.m_lose()
            end
            return
        end
    end
    for i=1,#self.m_data do
        if self.m_data[i]==1 and not self.m_isOpened[i] then --存在是O却没有被点击
            isSuccess = false
            break
        end
    end
    if isSuccess then
        -- print("成功了")
        if self.m_success and type(self.m_success)=="function" then
            self._touchAble = false
            self.m_success()
        end
    end
end

function M:clearGirds()
    self.super.clearGirds(self)
    for i=1,#self.m_data do
        self.m_isOpened[i] = false
    end
end

function M:runNextLevelAni()
    local duration = 0.5
    for i=1,#self.m_girds do
        local ani = transition.sequence{cc.ScaleTo:create(duration,0),cc.CallFunc:create(function(  )
            self:setTex_Default(self.m_girds[i])
            self.m_isOpened[i] = false
        end),cc.ScaleTo:create(0,1),cc.FadeOut:create(0),cc.FadeIn:create(duration/2)}
        self.m_girds[i]:runAction(ani)
    end
    return self
end

function M:onEnter()

end

function M:onExit()

end

return M